package bb.lanxing.lib.devices.utils;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import bb.lanxing.lib.devices.antplus.AntPlusDataDispatcher;
import bb.lanxing.lib.devices.base.converter.HeartrateConverter;

public class HeartrateAlert extends HeartrateConverter implements AntPlusDataDispatcher.OnAntPlusHeartrateListener, AudioManager.OnAudioFocusChangeListener, SoundPool.OnLoadCompleteListener {
    private static final int INVALID_PLAY_ID = 0;
    private int alertValued;
    AudioManager audioManager;
    private Context context;
    private boolean enabled;
    private boolean loaded;
    private boolean shouldBeAlert;
    private int soundIdLoaded;
    SoundPool soundPool;
    private int soundRawId;
    private int playId = 0;
    private Runnable alertRunnable = new Runnable() {
        @Override
        public void run() {
            if (HeartrateAlert.this.shouldBeAlert) {
                HeartrateAlert heartrateAlert = HeartrateAlert.this;
                heartrateAlert.playId = heartrateAlert.soundPool.play(HeartrateAlert.this.soundIdLoaded, 1.0f, 1.0f, 0, 0, 1.0f);
                HeartrateAlert.this.alertHandler.postDelayed(HeartrateAlert.this.alertRunnable, 1000L);
            }
        }
    };
    private Handler alertHandler = new AlertHandler(Looper.getMainLooper());

    public HeartrateAlert(Context context) {
        this.context = context;
    }

    public void setSoundRawId(int i) {
        this.soundRawId = i;
    }

    public void setEnabled(boolean z) {
        this.enabled = z;
    }

    public void setAlertValue(int i) {
        this.alertValued = i;
    }

    private void initAudio() {
        synchronized (this) {
            if (this.soundPool == null && this.enabled) {
                if (Build.VERSION.SDK_INT >= 21) {
                    this.soundPool = new SoundPool.Builder().setAudioAttributes(
                            new AudioAttributes.Builder()
                                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC).
                                    setLegacyStreamType(3).
                                    setUsage(AudioAttributes.USAGE_MEDIA).build()).build();
                } else {
                    this.soundPool = new SoundPool(3, 3, 100);
                }
                this.soundPool.setOnLoadCompleteListener(this);
                this.audioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
                this.soundIdLoaded = this.soundPool.load(this.context, this.soundRawId, 1);
            }
        }
    }

    @Override
    public void onHeartrate(int i, int i2) {
        if (!this.enabled) {
            return;
        }
        if (i2 >= this.alertValued) {
            this.shouldBeAlert = true;
            if (isPlaying()) {
                return;
            }
            play();
        } else if (!isPlaying()) {
        } else {
            this.shouldBeAlert = false;
            stop();
        }
    }

    private boolean isPlaying() {
        return (this.soundPool == null || this.playId == 0) ? false : true;
    }

    public void play() {
        initAudio();
        if (!this.loaded || !this.shouldBeAlert || !this.enabled || this.audioManager.requestAudioFocus(this, 3, 3) != 1) {
            return;
        }
        this.alertHandler.post(this.alertRunnable);
    }

    public void stop() {
        this.alertHandler.removeCallbacks(this.alertRunnable);
        this.playId = 0;
        this.audioManager.abandonAudioFocus(this);
    }

    public void release() {
        SoundPool soundPool = this.soundPool;
        if (soundPool != null) {
            soundPool.release();
        }
        AudioManager audioManager = this.audioManager;
        if (audioManager != null) {
            audioManager.abandonAudioFocus(this);
        }
        this.alertHandler.removeCallbacks(this.alertRunnable);
        this.alertHandler = null;
        this.audioManager = null;
        this.loaded = false;
        this.soundPool = null;
        this.playId = 0;
    }

    @Override
    public void onAudioFocusChange(int i) {
        if (i == 1 || i == 2 || i == 3 || i == 4) {
            return;
        }
        stop();
    }

    @Override
    public void onLoadComplete(SoundPool soundPool, int i, int i2) {
        boolean z = this.soundIdLoaded == i && i2 == 0;
        this.loaded = z;
        if (z) {
            play();
        }
    }


    private static class AlertHandler extends Handler {
        AlertHandler(Looper looper) {
            super(looper);
        }
    }
}
